package com.example.baidu;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class FindActivity extends AppCompatActivity {
    private MapView mMapView = null;
    private BaiduMap mBaiduMap;
    private SearchView mSearchView;
    private ListView mListView;
    private List<String> suggestions;
    private SuggestionSearch mSuggestionSearch;
    private List<SuggestionResult.SuggestionInfo> temp;
    private List<Overlay> overlays;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find);
        //获取地图对象
        mMapView = (MapView) findViewById(R.id.findMapView);
        Log.e("sss", "加载完成");
        mBaiduMap=mMapView.getMap();
        overlays=new ArrayList<>();

        //一个空的结果信息展示列表
        mListView=findViewById(R.id.listView);
        suggestions=new ArrayList<>();
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, suggestions);
        mListView.setAdapter(arrayAdapter);
        //列表点击监听
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.e(String.valueOf(i),String.valueOf(l));
                mListView.setVisibility(View.INVISIBLE);
                MapStatus.Builder builder= new MapStatus.Builder();
                builder.target(temp.get(i).getPt()).zoom(17);
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }
        });



        //百度地图搜索监听
        mSuggestionSearch = SuggestionSearch.newInstance();
        OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
            @Override
            public void onGetSuggestionResult(SuggestionResult suggestionResult) {
                if(overlays!=null&&overlays.size()>0){
                    mBaiduMap.removeOverLays(overlays);
                }
                mListView.setVisibility(View.VISIBLE);
                //处理sug检索结果
                temp =suggestionResult.getAllSuggestions();
                List<String> keys=new ArrayList<>();
                for (int i = 0; i < temp.size(); i++) {
                    Log.e("TAG",temp.get(i).toString());
                    keys.add(temp.get(i).getKey());
                    addPoint(temp.get(i).getPt(),temp.get(i).getKey());
                }
                //监听结果更新列表
                arrayAdapter.clear();
                arrayAdapter.addAll(keys);
                arrayAdapter.notifyDataSetChanged();
            }
        };
        mSuggestionSearch.setOnGetSuggestionResultListener(listener);


        //搜索组件
        mSearchView=findViewById(R.id.searchView);
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            //点击搜索按钮
            @Override
            public boolean onQueryTextSubmit(String s) {
                Log.e("TAG",s);

               //调用百度地图搜索
                mSuggestionSearch.requestSuggestion(new SuggestionSearchOption()
                        .city("北京")
                        .keyword(s));
                return false;
            }
            //文字改变
            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });
    }


    private void addPoint(LatLng point,String name){
        if(point==null){
            return;
        }

        TextView popup = new TextView(getApplicationContext());
        popup.setBackgroundColor(Color.WHITE);
        popup.setText(name);
        popup.setTextColor(Color.BLACK);

//构造InfoWindow
//point 描述的位置点
//-100 InfoWindow相对于point在y轴的偏移量


//构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.locate);

//构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .infoWindow(new InfoWindow(popup, point, -100))
                .scaleX(0.5f)
                .scaleY(0.5f)
                .icon(bitmap);
//在地图上添加Marker，并显示
        overlays.add(mBaiduMap.addOverlay(option));
    }
}